﻿package Code {	
	import flash.display.Shape;
	
	public class Bar extends Shape{
		
		protected var thickness:Number;                    //thickness of the bar
		public var a:Vertex;								//the top left corner vertex
		public var b:Vertex;								//the top right corner vertex
		public var origin:Vertex;							//(0,0) for calculations
		public var vx:Number;								//velocity in the X direction
		public var vy:Number;								//velocity in the Y direction
		protected var color:uint;							//color of the Bar
		private var G:Number = 1000;      		          // gravity (acceleration g)
		private var w:Number = 0;  		                 // initial angular velocity
		private var aAccel:Number = 0;             		  // angular acceleration
		private var theta:Number;						  // current angle
		
		//constructor
		public function Bar(a:Vertex, b:Vertex, thickness:Number = 10, color:uint= 0x880088) {
			this.a = a;
			this.b = b;
			this.thickness = thickness;
			this.color = color;
			origin = new Vertex(0,0);
			vx = 0;
			vy = 0;
			init();
		}
		
		//initialize
		public function init() {
			graphics.clear();
			graphics.lineStyle(thickness, color);
			graphics.moveTo(a.x, a.y);
			graphics.lineTo(b.x, b.y);
		}
		
		//we procrastinated and didn't have time to figure out the angular momentum, moment of inertia, torque etc so for now this is a stub
		public function update():void {
			//var cos = Math.cos(theta);
			
			//acceleration from torque in the Y direction
			//var ayR:Number =  G*cos - /*(K/M)*/(b.y-a.y)*cos;
		}
		
		// gets theta (from a to b) in radians:
		public function getAngleBA():Number {
			return getAngle(b,a)
		}
		
		// gets theta (from b to a) in radians:
		public function getAngleAB():Number {
			return getAngle(a,b)
		}
		
		// gets angle 0 to a in radians:
		public function getAngleA():Number {
			return getAngle(origin,a);
		}
		
		// gets angle 0 to b in radians:
		public function getAngleB():Number {
			return getAngle(origin,b);
		}
		
		// get angle between q & p:
		private function getAngle(p:Vertex,q:Vertex):Number {
			var xLength:Number = q.x - p.x;
			var yLength:Number = q.y - p.y;
			return Math.atan2(yLength, xLength);
		}
		
		//returns rotated Vertex by the angle sent
		private function rotate(old:Vertex,angle:Number):Vertex {
			var sin:Number = Math.sin(angle);
			var cos:Number = Math.cos(angle);
			var Xnew:Number = cos*old.x-sin*old.y;
			var Ynew:Number = sin*old.x+cos*old.y;
			return new Vertex(Xnew,Ynew);
		}
	}
}	
